package Client;

import SQL.Connections;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class TicketRecord extends JFrame{
    private JPanel panel1;
    private JButton 查看详情Button;
    private JButton 退票Button;
    private JButton 站站查询Button;
    private JButton 购票记录Button;
    private JTable table1;
    public static JFrame f;

    static String classno=new String(PassengerHome.user1);//手机号
    static String classno1;
    Vector<Vector> values;
    Vector<String> name;
    private Statement stm;
    public static String a = new String("");

    public TicketRecord() {
        站站查询Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //跳转
                PassengerHome passengerHome=new PassengerHome();
                passengerHome.initialize(classno);
                f.dispose();
            }
        });
        查看详情Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //跳转
                PassengerTicketpage passengerTicketpage=new PassengerTicketpage();
                passengerTicketpage.initialize(a);
                f.dispose();
            }
        });
    退票Button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int n=JOptionPane.showConfirmDialog(null,"是否退票","退票",JOptionPane.YES_NO_OPTION);
            if(n==0){//0为是
                try {
                    String sql=("delete from ticket where TClassNumber='"+classno1+"'");//退票
                    Connections connections = new Connections();
                    Connection con = connections.getConnection();
                    stm = con.createStatement();
                    int re = stm.executeUpdate(sql);

                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                JOptionPane.showMessageDialog(null,"退票成功",null,JOptionPane.PLAIN_MESSAGE);
            }else{

            }
        }
    } );

        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                int rowINdex=table1.getSelectedRow();
                int Column=table1.getColumnCount();
                classno1= (String) values.get(rowINdex).get(1);
            }
        });
    }


    public static void main(String[] args) {
        initialize();


    }
    public static void initialize(){
        initialize(classno);
    }

    static void initialize(String no) {
        JFrame frame = new JFrame("Ticket");
        frame.setContentPane(new TicketRecord().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);//设置屏幕居中
        frame.setVisible(true);
        f=frame;


    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
        String sql1=("SELECT IdCardNumber FROM usertrain WHERE UserPhoneNumber='"+classno+"'");

        try {
            //Vector<Vector> i= Connections.queryData(sql1);
            Connections connections = new Connections();
            Connection con = connections.getConnection();
            Statement stm = con.createStatement();
            ResultSet rsClasses = stm.executeQuery(sql1);
            while(rsClasses.next()){
                a=rsClasses.getString("IdCardNumber");

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        System.out.println(a);
        String sql=("SELECT * FROM ticket WHERE TIdCardNumber='"+a+"'");
        values=new Vector<>();
        name=new Vector();
        values= Connections.queryData(sql);
        name.add("车牌号");
        name.add("班次号");
        name.add("座位号");
        name.add("购买人身份证");
        name.add("价钱");
        TableModel d=new DefaultTableModel(values,name);
        table1= new JTable(values,name);
        //table1.setRowSelectionInterval(0,0);

    }
}
